1.2 Parser:Bridging Tokens and Syntax with Listeners

In the previous article 1.1 Scanner:from String to Tokens, we discussed how Dart source code is parsed into a sequence of Tokens by the Scanner.

In this article, we will continue to explore how the Parser processes the Token sequence further. We are steadily progressing towards the Dart Abstract Syntax Tree.

parser/parser.dart is the entry point of this module, providing a parse method that accepts a Token instance (a linked list of Tokens) to execute parsing operations.

The parsing is performed by the Parser class. This class is part of a hierarchy, with the base class being Parser, and its subclasses including ClassMemberParser, MiniAstParser, and TopLevelParser.

The key method of the Parser class is parseUnit. Inside, it contains a loop that traverses the Token linked list. Within this loop, a substantial number of layered rules are established. Based on the relationships between Tokens, these rules identify basic syntax structures.

Does this process resemble tokenize? They share a common pattern, but the difference lies in their objects of operation: one works on characters, the other on Tokens, and their rules are also vastly different.

After this second processing, we can now recognize Dart syntax elements like the start of a method or method body. Inside the Parser, there is a property of type Listener. The Parser calls corresponding methods in the Listener upon encountering a syntax, serving as a notifier. The Listener defines numerous methods (one for each Dart syntax event).

The Listener class is the key connection between the Parser and subsequent processes. What's next? Building the Abstract Syntax Tree (AST), which is done by Dart's Fasta parser. Although the implementation of Fasta is complex, at its core, it is based on the Listener and driven by its multitude of events.


本文作者:Maeiee

本文链接:1.2 Parser:Bridging Tokens and Syntax with Listeners

版权声明:如无特别声明,本文即为原创文章,版权归 Maeiee 所有,未经允许不得转载!


喜欢我文章的朋友请随缘打赏,鼓励我创作更多更好的作品!